package cn.bocweb.spinning.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.QaMyAnswerBean;
import cn.bocweb.spinning.util.DeviceUtil;

/**
 * Created by chenchangxing on 15/12/28.
 */
public class MyAnswerAdapter extends RecyclerView.Adapter {

    OnItemClickListener onItemClickListener;
    Context mContext;

    List<QaMyAnswerBean.DataBean> mDataBeanList;

    public MyAnswerAdapter(List<QaMyAnswerBean.DataBean> mDataBeanList) {
        this.mDataBeanList = mDataBeanList;
    }

    public interface OnItemClickListener {
        public void onClick(int position);
    }

    public void setOnItemClick(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_my_answer, null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ViewHolder myHolder = (ViewHolder)holder;
        myHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.onClick(position);
            }
        });
        myHolder.myAnswerTitle.setText(mDataBeanList.get(position).getContent());
        if (mDataBeanList.get(position).getIscheck().equals("1")) {
            myHolder.myAnswerState.setText("已解决");
            myHolder.myAnswerState.setTextColor(mContext.getResources().getColor(R.color.green_ques));
        } else {
            myHolder.myAnswerState.setText("未解决");
            myHolder.myAnswerState.setTextColor(mContext.getResources().getColor(R.color.red));
        }
        myHolder.myAnswerTime.setText(
                DeviceUtil.getTimeByTwoTimeStampForPhp(
                        Long.parseLong(mDataBeanList.get(position).getTimeline()) * 1000,
                        System.currentTimeMillis()));
    }

    @Override
    public int getItemCount() {
        return mDataBeanList.size();
    }


    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'item_my_answer.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */
    static class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.my_answer_title)
        TextView myAnswerTitle;
        @Bind(R.id.my_answer_state)
        TextView myAnswerState;
        @Bind(R.id.my_answer_time)
        TextView myAnswerTime;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
